package game.engine.base.input

import game.engine.base.Scene
import game.engine.base.Touch

class InputManager(val scene: Scene) {

    // Input事件管理
    var touchLstInput = ArrayList<Touch>()
    var touchLstCopy = ArrayList<Touch>()

    val locker = Object()

    fun update() {
        synchronized(locker) {
            val copyRef = touchLstCopy
            touchLstCopy = touchLstInput
            touchLstInput = copyRef
            touchLstInput.clear()
        }
        for (i in 0 until touchLstCopy.size){
            if (scene.willDestroy){
                break
            }
            scene.onTouch(touchLstCopy[i])
        }
        touchLstCopy.clear()
    }

    fun addTouch(touch: Touch) {
        synchronized(locker) {
            touchLstInput.add(touch)
        }
    }

    fun release(){
        synchronized(locker){
            touchLstInput.clear()
        }
        touchLstCopy.clear()
    }
}